package com.retail.help.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HtmlUtil;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.core.entity.result.Result;
import com.retail.help.entity.vo.RetailHelpVo;
import com.retail.core.entity.ParamReq;
import com.retail.help.entity.request.RetailHelpRequest;
import com.retail.help.mapper.RetailHelpMapper;
import com.retail.help.service.RetailHelpService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 帮助文档 Service impl
 *
 * @author Administrator
 */
@Service
public class RetailHelpServiceImpl implements RetailHelpService {

    @Resource
    private RetailHelpMapper retailHelpMapper;

    @Override
    public RetailHelpVo getModel(Long id) {
        RetailHelpVo retailHelpVo = retailHelpMapper.getModel(id);
        if (ObjectUtil.isNotEmpty(retailHelpVo)) {
            String helpDesc = retailHelpVo.getHelpContent();
            if (ObjectUtil.isNotEmpty(helpDesc)) {
                String newHelpDesc = HtmlUtil.filter(helpDesc);
                if (ObjectUtil.isNotEmpty(newHelpDesc) && newHelpDesc.length() > 100) {
                    retailHelpVo.setHelpDesc(newHelpDesc + "...");
                } else {
                    retailHelpVo.setHelpDesc(newHelpDesc);
                }
            }
        }

        return retailHelpVo;
    }

    @Transactional
    @Override
    public Long save(RetailHelpRequest retailHelpRequest) {
        retailHelpMapper.save(retailHelpRequest);
        return retailHelpRequest.getId();
    }

    @Transactional
    @Override
    public Long update(RetailHelpRequest retailHelpRequest) {
        retailHelpMapper.update(retailHelpRequest);
        return retailHelpRequest.getId();
    }

    @Override
    public List<RetailHelpVo> listPage(ParamReq<RetailHelpRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        List<RetailHelpVo> retailHelpVos = retailHelpMapper.listPage(params);
        retailHelpVos.forEach(retailHelpVo -> {
            if (ObjectUtil.isNotEmpty(retailHelpVo)) {
                String helpDesc = retailHelpVo.getHelpContent();
                if (ObjectUtil.isNotEmpty(helpDesc)) {
                    String newHelpDesc = HtmlUtil.filter(helpDesc);
                    if (ObjectUtil.isNotEmpty(newHelpDesc) && newHelpDesc.length() > 100) {
                        retailHelpVo.setHelpDesc(newHelpDesc + "...");
                    } else {
                        retailHelpVo.setHelpDesc(newHelpDesc);
                    }
                }
            }
        });

        return retailHelpVos;
    }

    @Override
    public Integer countPage(ParamReq<RetailHelpRequest> params) {
        return retailHelpMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return retailHelpMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public List<RetailHelpVo> findHelps() {
        //return null;
      return retailHelpMapper.findHelps();
    }

    @Override
    public Result<RetailHelpVo> getXieyiRetailHelp(String xieyiType) {
        Integer type = getXieyiRetail(xieyiType);
        RetailHelpVo retailHelpVo = retailHelpMapper.getXieyiRetailHelp(type);
        if (ObjectUtil.isEmpty(retailHelpVo)) {
            retailHelpVo = new RetailHelpVo();
        }

        return Result.success(retailHelpVo);
    }

    @Override
    public Result<Integer> insertXieyiRetailHelp(RetailHelpRequest request) {
        Integer type = getXieyiRetail(request.getXieyiType());
        RetailHelpVo retailHelpVo = retailHelpMapper.getXieyiRetailHelp(type);
        if (ObjectUtil.isNotEmpty(retailHelpVo)) {
            request.setId(retailHelpVo.getId());
            retailHelpMapper.update(request);
        } else {
            request.setHelpType(type);
            request.setStatusFlag(StatusFlagEnum.USE.getValue());
            request.setDelFlag(DelFlagEnum.NO.getValue());
            retailHelpMapper.save(request);
        }

        return Result.success("更新成功", 1);
    }

    private Integer getXieyiRetail(String xeiyiType) {
        Map<String, Integer> ret = new HashMap<>();
        ret.put("txxieyi", 2);
        ret.put("yinsizhengce", 3);
        ret.put("fuwuxieyi", 4);
        ret.put("tixianshuoming", 5);
        ret.put("fenchengshuoming", 6);
        return ret.get(xeiyiType);
    }
}
